package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.domain.user.identify.PersCertChannelDTL;
import com.gopay.common.user.dao.PersCertChannelDtlDao;

@Repository("persCertChannelDtlDao")
public class PersCertChannelDtlDaoImpl extends BaseDAOImpl<PersCertChannelDTL, Serializable> implements PersCertChannelDtlDao {

    @Override
    public PersCertChannelDTL addCertChannelDtl(CustPersonInfo cust, String channel, boolean isSuccess) {
        PersCertChannelDTL obj = new PersCertChannelDTL();
        obj.setCustId(cust.getCustId());
        obj.setCertNo(cust.getCertNo());
        obj.setChannelCode(channel);
        obj.setStatus(isSuccess == true ? "01" : "02");
        obj.setCreatedTs(new Date());
        PersCertChannelDTL save = save(obj);
        return save;
    }

    @Override
    public int findSuccessCertChannelCountsByCustId(String custId) {
        //生产优化14期 将 原有4% 替换为 4_ ，修改原因 4开头的人脸识别被去除，导致403央行银行代码的银行卡认证不计数；原本枚举中无银行以4开头常量，只有4开头人脸识别，现去除4开头两位数字渠道
        String hql = "select count(*) From PersCertChannelDTL where custId=:custId and status = '01' and channelCode not like '4_' ";//生产bug6期 去掉人脸识别 lx 2017-6-26 13:49:32
        
        Map param = new HashMap();
        param.put("custId", custId);
        
        int count = findCountByHql(hql, param);
        return count;
    }

    @Override
    public boolean hasDoQueryCertificationSuccess(CustPersonInfo cust) {
        DetachedCriteria criteria = DetachedCriteria.forClass(PersCertChannelDTL.class);
        criteria.add(Restrictions.eq("custId", cust.getCustId()))
        .add(Restrictions.eq("channelCode", "02"))
        .add(Restrictions.eq("status", "01"));
        
        return findByCriteria(criteria).size() > 0;
    }

    @Override
    public boolean hasDoQueryCertification(CustPersonInfo cust) {
        DetachedCriteria criteria = DetachedCriteria.forClass(PersCertChannelDTL.class);
        criteria.add(Restrictions.eq("custId", cust.getCustId()))
        .add(Restrictions.eq("channelCode", "02"));
        
        return findByCriteria(criteria).size() > 0;
    }

    @Override
    public PersCertChannelDTL findByCustIdAndChannel(String custId, String channelCode) {
        String hql="from PersCertChannelDTL p where p.custId=:custId and p.channelCode=:channelCode and p.status='01'";
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("custId", custId);
        param.put("channelCode", channelCode);
        List<PersCertChannelDTL> list = findByHql(hql, param);
        if(null==list||list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public PersCertChannelDTL findByCardNoAndChannel(String custId, String cardNo, String channelCode){
    	/**
    	 * modified by zyt,敏感字段的查询使用in
    	 * 2017-1-13
    	 */
    	//start+++++++++
    	List<String> hisNoList = SensitiveInfoUtils.getAllHisCipherText(cardNo);
    	String hql="from PersCertChannelDTL p where p.custId=:custId and p.cardNo in (:hisNoList)  and p.channelCode=:channelCode and p.status='01'";
        //String hql="from PersCertChannelDTL p where p.custId=:custId and p.cardNo=:cardNo and p.channelCode=:channelCode and p.status='01'";

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("custId", custId);
        param.put("hisNoList", hisNoList.toArray());
        //param.put("cardNo", cardNo);
        //end++++++++++++
        param.put("channelCode", channelCode);
        List<PersCertChannelDTL> l = this.findByHql(hql, param);
        if(null!=l&&!l.isEmpty()){
            return l.get(0);
        }
        return null;
    }

    @Override
    public PersCertChannelDTL findByCardNoAndChannel(String custId, String cardNo){
    	/**
    	 * modified by zyt,敏感字段的查询使用in
    	 * 2017-1-13
    	 */
    	//start+++++++++
    	List<String> hisNoList = SensitiveInfoUtils.getAllHisCipherText(cardNo);
        String hql="from PersCertChannelDTL p where p.custId=:custId and p.cardNo in (:hisNoList)  and p.status='01'";
        //String hql="from PersCertChannelDTL p where p.custId=:custId and p.cardNo=:cardNo  and p.status='01'";

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("custId", custId);
        param.put("hisNoList", hisNoList.toArray());
        //param.put("cardNo", cardNo);
        //end+++++++++++++
        List<PersCertChannelDTL> l = this.findByHql(hql, param);
        if(null!=l&&!l.isEmpty()){
            return l.get(0);
        }
        return null;
    }
    
}
